package com.wit.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Hibernate;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;

import com.opensymphony.xwork2.ActionSupport;
import com.wit.po.Department;
import com.wit.po.Employee;
import com.wit.po.Separation;
import com.wit.service.SeparationService;
import com.wit.util.Pager;

@SuppressWarnings("serial")
public class SeparationAction extends ActionSupport{

	private Separation separation;
	
	private SeparationService separationService;
	
	private Pager pager;
	
	private List<Department> departmentList;
	
	private List<Employee> employeeList;
	
	private List<Separation> separationList;
	
	private Date separationDateStart;
	
	private Date separationDateEnd;

	public Date getSeparationDateStart() {
		return separationDateStart;
	}

	public void setSeparationDateStart(Date separationDateStart) {
		this.separationDateStart = separationDateStart;
	}

	public Date getSeparationDateEnd() {
		return separationDateEnd;
	}

	public void setSeparationDateEnd(Date separationDateEnd) {
		this.separationDateEnd = separationDateEnd;
	}

	public List<Separation> getSeparationList() {
		return separationList;
	}

	public void setSeparationList(List<Separation> separationList) {
		this.separationList = separationList;
	}

	public List<Department> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<Department> departmentList) {
		this.departmentList = departmentList;
	}

	public List<Employee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<Employee> employeeList) {
		this.employeeList = employeeList;
	}

	public Separation getSeparation() {
		return separation;
	}

	public void setSeparation(Separation separation) {
		this.separation = separation;
	}

	public SeparationService getSeparationService() {
		return separationService;
	}

	public void setSeparationService(SeparationService separationService) {
		this.separationService = separationService;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}
	
	public String addSeparation(){
		
		separationService.addSeparation(separation);
		departmentList = separationService.findAllDepartment();
		separation = separationService.findSeparationById(separation.getSeparationId());
		employeeList = separationService.findEmployeeByDepartmentId(separation.getEmployee().getDepartment().getDepartmentId());
		return "addPage";
	}
	
	public String deleteSeparation(){
		separation = separationService.findSeparationById(separation.getSeparationId());
		separationService.deleteSeparation(separation);
		separation = null;
		return "listPage";
	}
	
	public String modifySeparation(){
		separationService.modifySeparation(separation);
		departmentList = separationService.findAllDepartment();
		System.out.println(separation.getEmployee().getEmployeeId());
		separation = separationService.findSeparationById(separation.getSeparationId());
		System.out.println(separation.getEmployee().getDepartment());
		employeeList = separationService.findEmployeeByDepartmentId(separation.getEmployee().getDepartment().getDepartmentId());
		return "editPage";
	}
	
	public String findSeparationByCondition(){
		DetachedCriteria dc = DetachedCriteria.forClass(Separation.class);
		dc.createAlias("employee", "employee");
		DetachedCriteria dcCount = DetachedCriteria.forClass(Separation.class);
		dcCount.createAlias("employee", "employee");
		if(separation.getEmployee().getEmployeeId() != null && !separation.getEmployee().getEmployeeId().equals("")){
			System.out.println("employeeId"+separation.getEmployee().getEmployeeId());
			dc.add(Property.forName("employee.employeeId").like("%"+separation.getEmployee().getEmployeeId()+"%"));
			dcCount.add(Property.forName("employee.employeeId").like("%"+separation.getEmployee().getEmployeeId()+"%"));
		}
		if(separation.getEmployee().getEmployeeNameC()!=null && !separation.getEmployee().getEmployeeNameC().equals("")){
			dc.add(Property.forName("employee.employeeNameC").like("%"+separation.getEmployee().getEmployeeNameC()+"%"));
			dcCount.add(Property.forName("employee.employeeNameC").like("%"+separation.getEmployee().getEmployeeNameC()+"%"));
		}
		if(this.getSeparationDateStart() != null && this.getSeparationDateEnd() != null){
			dc.add(Property.forName("separationDate").between(this.getSeparationDateStart(), this.getSeparationDateEnd()));
			dcCount.add(Property.forName("separationDate").between(this.getSeparationDateStart(), this.getSeparationDateEnd()));
		}
		dcCount.setProjection(Property.forName("separationId").count());
		if(pager.getPageCurrent() == null){
			pager.setPageCurrent(1);
		}
		int count = separationService.findSeparationCount(dcCount);
		pager.setRecordCount(count);
		pager.setPageCount(count%pager.getPageSize() == 0? count/pager.getPageSize():count/pager.getPageSize()+1);
		if(pager.getPageCurrent() == null || pager.getPageCurrent() > pager.getPageCount()){
			pager.setPageCurrent(1);
		}
		separationList = separationService.findSeparationByCondition(dc, (pager.getPageCurrent()-1)*pager.getPageSize(), pager.getPageSize());
		return "listPage";
	}
	
	public String findSeparationById(){
		System.out.println("separationId:"+separation.getSeparationId());
		separation = separationService.findSeparationById(separation.getSeparationId());
		departmentList = separationService.findAllDepartment();
		employeeList = separationService.findEmployeeByDepartmentId(separation.getEmployee().getDepartment().getDepartmentId());
		return "editPage";
	}
	
	public String initSeparation(){
		departmentList = separationService.findAllDepartment();
		System.out.println(departmentList.size());
		employeeList = new ArrayList<Employee>();
		return "addPage";
	}
	
	public String findEmployeeByDepartmentId(){
		System.out.println("in find employee:"+separation.getEmployee().getDepartment().getDepartmentId());
		employeeList = separationService.findEmployeeByDepartmentId(separation.getEmployee().getDepartment().getDepartmentId());
		StringBuilder sb = new StringBuilder("[");
		for(int i = 0; i < employeeList.size(); i ++){
			sb.append("{");
			sb.append("employeeId:\""+employeeList.get(i).getEmployeeId()+"\"");
			sb.append(",");
			sb.append("employeeName:\""+employeeList.get(i).getEmployeeNameC()+"\"");
			sb.append("}");
			if(i != employeeList.size() - 1)
				sb.append(",");
		}
		sb.append("]");
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/json;charset=gbk");
		response.setHeader("cache-control", "no-cache");
		System.out.println(sb.toString());
		try{
		PrintWriter out = response.getWriter();
		out.print(sb);
		out.flush();
		out.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
}
